package com.hjimi.ski.view.widget;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import com.hjimi.ski.R;
import razerdp.basepopup.BasePopupWindow;
import xyz.doikki.videocontroller.StandardVideoController;
import xyz.doikki.videoplayer.player.VideoView;

/**
 * Created By 孙岩
 * on 2023/10/13
 */
public class VideoPopup extends BasePopupWindow {
    private VideoView player;
    private StandardVideoController controller;
    public VideoPopup(Context context, String url) {
        super(context);
        player = (VideoView) findViewById(R.id.player);
        startVideo(url,context);
    }

    public void startVideo(String url,Context context){
        Log.d("sunyan","播放的url="+url);
        player.setUrl(url); //设置视频地址
        StandardVideoController controller = new StandardVideoController(context);
        controller.addDefaultControlComponent("", false);
        player.setVideoController(controller); //设置控制器
        //监听播放结束
        player.addOnStateChangeListener(new VideoView.SimpleOnStateChangeListener() {

            @Override
            public void onPlayStateChanged(int playState) {
                if (playState == VideoView.STATE_PLAYBACK_COMPLETED) {
                    player.release();
                    //重新设置数据
                    player.setUrl(url); //设置视频地址
                    player.setVideoController(controller);
                    //开始播放
                    player.start();
                } else if (playState == VideoView.STATE_ERROR) {
                    Log.d("sunyan", playState + "");
                }
            }
        });
        player.start(); //开始播放，不调用则不自动播放

    }

    public void stopVideo(){
        if(player!=null){
            player.release();
        }
    }

    @Override
    protected Animation initShowAnimation() {
        return null;
    }

    @Override
    public View getClickToDismissView() {
        return getPopupWindowView();
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.popup_video);
    }

    @Override
    public View initAnimaView() {
        return findViewById(R.id.rlStart);
    }
}
